import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

const store=new Vuex.Store({
    state: {
        playList: [{
            al: {
                id: 155361528,
                name: "随风",
                pic: 109951168082466600,
                picUrl: "https://p1.music.126.net/U8pfkpTKk_mhkQzfYWZDRw==/109951168082466587.jpg",
                pic_str: "109951168082466587"
            },
            id: 2000771305,
            ar: [
                {
                    name: '陈钰羲'
                }
            ],
            name: "随风",
        }],
        index: 0,
        isShow: true,
        show: false,
        lyric: '',
        lyricArr: [],
        currentTime: 0,
        duration: 0,
        searchValue: '',
        searchList: []
    },
    mutations: {
        getPlayList_vuex(state,payload) {
            state.playList=payload;
        },
        changeIndex_vuex(state,payload) {
            state.index=payload;
        },
        changeIsShow(state,payload) {
            state.isShow=payload;
        },
        changeShow(state,payload) {
            state.show=payload;
        },
        getLyric(state,payload) {
            state.lyric=payload;
        },
        getLyricArr(state,payload) {
            state.lyricArr=payload;
            //console.log(payload);
        },
        updateCurrentTime(state,payload) {
            state.currentTime=payload;
        },
        getDuration(state,payload) {
            state.duration=payload;
        },
        getSearchList(state,payload) {
            state.searchList=payload;
        }
    },
    actions: {
        async getLyricData(context) {
            await axios.get(`/lyric?id=${context.state.playList[context.state.index].id}`).then(res=>{
              //console.log(res);
              context.commit('getLyric',res.data.lrc.lyric);
            }).catch(err=>{
              console.log(err);
            })
        },
        async getSearchRes(context,value) {
            await axios.get(`/cloudsearch?keywords=${value}`).then(res=>{
              context.commit('getSearchList',res.data.result.songs);
             // console.log(res);
            }).catch(err=>{
              console.log(err);
            })
          }
    },
    modules: {

    }
})

export default store